﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the  interface.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Common;
    using Data.Interfaces;
    using Entities;
    using Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Business.TagManager"/> type.
    /// </summary>
    public class TagManager : ITagManager
    {
        /// <summary>
        /// Tag repository
        /// </summary>
        private readonly ITagRepository _tagRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="TagManager"/> class.
        /// </summary>
        public TagManager()
            : this(ServiceLocator.Current.GetInstance<ITagRepository>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TagManager"/> class.
        /// </summary>
        /// <param name="tagRepository">
        /// The resolve.
        /// </param>
        public TagManager(ITagRepository tagRepository)
        {
            Contract.Requires<ArgumentNullException>(tagRepository != null);
            _tagRepository = tagRepository;
        }

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IEnumerable<Tag> List(Tag filter, int? pageNo, int? pageSize)
        {
            return _tagRepository.Query(filter.Map<Tag, Data.Entities.Tag>(), pageNo, pageSize)
                .ToList()
                .Select(x => x.Map<Data.Entities.Tag, Tag>());
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>Id of tag added</returns>
        public int Add(Tag entity)
        {
            return _tagRepository.Add(entity.Map<Tag, Data.Entities.Tag>());
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(Tag entity)
        {
            _tagRepository.Update(entity.Map<Tag, Data.Entities.Tag>());
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(Tag entity)
        {
            _tagRepository.Delete(entity.Map<Tag, Data.Entities.Tag>());
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id of tag</param>
        /// <returns>Tag matching the id</returns>
        public Tag GetById(int id)
        {
            return _tagRepository.FindById(id).Map<Data.Entities.Tag, Tag>();
        }

        /// <summary>
        /// Gets the tags by transaction.
        /// </summary>
        /// <param name="transactionId">The transaction id.</param>
        /// <returns>List of tags associated with given transaction id</returns>
        public IEnumerable<Tag> GetTagsByTransaction(int transactionId)
        {
            return _tagRepository.GetTagsForTransaction(transactionId)
                .Select(x => x.Map<Data.Entities.Tag, Tag>())
                .ToList();
        }

        /// <summary>
        /// Gets the name of the by.
        /// </summary>
        /// <param name="tagName">Name of the tag.</param>
        /// <returns>Tag matching by name</returns>
        public Tag GetByName(string tagName)
        {
            return _tagRepository.Query(new Data.Entities.Tag { Name = tagName })
                  .FirstOrDefault()
                  .Map<Data.Entities.Tag, Tag>();
        }
    }
}